//
//  FSReleaseController.m
//  fanshu
//
//  Created by mh on 16/3/27.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSReleaseController.h"
#import "SVProgressHUD.h"
#import "FSCoverView.h"
#import "HWTextView.h"
#import "FSTopTool.h"


#define WH 44

@interface FSReleaseController ()<FSReleaseDelegate>

//内容输入框
@property (nonatomic, weak) HWTextView *textView;

//标题内容
@property (nonatomic, weak) HWTextView *titleView;

//关闭弹窗
@property (nonatomic, weak) UIView *bottomView;

//字数显示
@property (nonatomic, weak) UILabel *textCount;

@end

@implementation FSReleaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //添加顶部工具条
    [self setupTopView];

    //添加标题输入框和文本输入框
    [self setupTitleTextView];

    

}

-(void)textChange
{
    self.textCount.text = [NSString stringWithFormat:@"%zd字",self.textView.text.length];
}



#pragma mark - 添加顶部工具条
-(void)setupTopView
{
    FSTopTool *tool = [[FSTopTool alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, WH)];
    [tool setupTopView];
    tool.delegate = self;
    [self.view addSubview:tool];
    tool.releaseOnClick = ^{
        if ([self.delegate respondsToSelector:@selector(releaser:title:text:)]) {
            [self.delegate releaser:self title:self.titleView.text text:self.textView.text];
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    };
}

- (void)FSTopToolOutLoginBtnClick{
    [self outLoginBtnClick];
}


#pragma mark - 点击了退出按钮
-(void)outLoginBtnClick
{
    //弹出取消对话框
    FSCoverView *cover = [FSCoverView show];
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.frame = CGRectMake(0, ScreenHeight - 150 , ScreenWidth, 150);
    self.bottomView = bottomView;
    [cover addSubview:bottomView];
    
    //添加私密保存按钮
    UIButton *saveBtn = [self addOneButtonWithName:@"私密保存" color:[UIColor greenColor]];
    saveBtn.center = CGPointMake(bottomView.width * 0.5, 25);
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:saveBtn];
    
    //添加放弃编辑按钮
    UIButton *quitBtn = [self addOneButtonWithName:@"放弃编辑" color:[UIColor redColor]];
    quitBtn.center = CGPointMake(bottomView.width * 0.5, 75);
    [quitBtn addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:quitBtn];
    
    //添加取消按钮
    UIButton *cancelBtn = [self addOneButtonWithName:@"取消" color:[UIColor lightGrayColor]];
    cancelBtn.center = CGPointMake(bottomView.width * 0.5, 125);
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:cancelBtn];
    
    //分割线1
    UIView *line1 = [[UIView alloc] init];
    line1.frame = CGRectMake(10, 50, ScreenWidth - 20, 1);
    line1.backgroundColor = [UIColor lightGrayColor];
    [bottomView addSubview:line1];
    
    //分割线2
    UIView *line2 = [[UIView alloc] init];
    line2.frame = CGRectMake(10, 100, ScreenWidth - 20, 1);
    line2.backgroundColor = [UIColor lightGrayColor];
    [bottomView addSubview:line2];
}

#pragma mark - 取消
-(void)cancel
{
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 150);
    } completion:^(BOOL finished) {
        for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
            if ([view isKindOfClass:[FSCoverView class]]) {
                [view removeFromSuperview];
                [self.bottomView removeFromSuperview];
            }
        }
    }];

}

#pragma mark - 私密保存
-(void)save
{
    NSLog(@"点击了私密保存");
}

#pragma mark -  放弃编辑
-(void)quit
{
    //清楚弹窗
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 150);
    }completion:^(BOOL finished) {
            [self.bottomView removeFromSuperview];
    }];
    
    //添加弹窗
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"放弃编辑后,本次编辑的内容将会丢失,确定放弃?" preferredStyle:UIAlertControllerStyleAlert];
    
    //取消
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
            if ([view isKindOfClass:[FSCoverView class]]) {
                [view removeFromSuperview];
            }
        }
    }];
    
    //确定放弃编辑
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
        for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
            if ([view isKindOfClass:[FSCoverView class]]) {
                [view removeFromSuperview];
                [self.bottomView removeFromSuperview];
            }
        }
    }];
    
    //显示到窗口
    [alert addAction:action];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark - 添加一个按钮
-(UIButton *)addOneButtonWithName:(NSString *)name color:(UIColor *)color
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:name forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    CGRect frame = btn.frame;
    frame.size = CGSizeMake(80, 80);
    btn.frame = frame;
    return btn;
}


#pragma mark - 添加标题输入框
-(void)setupTitleTextView
{
    //设置标题输入框
    HWTextView *titleView = [[HWTextView alloc] initWithFrame:CGRectMake(5, 64, ScreenWidth - 10, 40)];
    titleView.placeholder = @"请输入标题";
    titleView.font = setFont(20.0f);
    titleView.placeholderColor = [UIColor lightGrayColor];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    //添加分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(titleView.frame), ScreenWidth - 10, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:line];
    
    //设置内容输入框
    HWTextView *textView = [[HWTextView alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(line.frame), ScreenWidth - 10, ScreenHeight)];
    textView.placeholder = @"请输入正文";
    textView.placeholderColor = [UIColor lightGrayColor];
    [self.view addSubview:textView];
    self.textView = textView;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
}


@end
